home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-12-24 | 4.1 KB | 228 lines | [TEXT/MSET] |
- \ BITSTRING class. May 88.
-
- need bytestring
-
-
- :code BLOCATE \ ( n b -- bit-index OR -1 )
-
- \ Bit locate. bit-index points to the 1st occurrence of bit b in n,
- \ proceeding from left to right. The leftmost bit has index zero
- \ (sensibly, unlike the usual 68000 convention). If the bit is not
- \ found we return -1.
- \ This code was lifted from the earlier PDP-11 version. DEC numbers
- \ bits the wrong way round, too. So does Intel. But IBM are OK!!!!
- \ (Alright, alright, so we're strictly big-endian around here.)
-
- loc
- MOVEQ #-1,D0 ; Initial result
- TST (SP)+
- BNE.S getn
- NOT (SP)
- getn MOVE (SP),D1
- BEQ.S end
- lp ADDQ #1,D0
- ROL #1,D1
- BCC.S lp
- end MOVE D0,(SP)
- ;code
-
-
- :class BITSTRING super( bytestring )
-
- int BP
- int BL
-
- :mcode BPOS:
- MOVE 8(A2),D0
- LSL #3,D0
- OR.W 18(A2),D0
- PUSH D0
- ;mcode
-
- :mcode BLIM:
- MOVE 12(A2),D0
- LSL #3,D0
- OR.W 20(A2),D0
- PUSH D0
- ;mcode
-
- :mcode >BPOS:
- POP D0
- MOVE D0,D1
- ANDI #7,D1
- MOVE.W D1,18(A2)
- LSR #3,D0
- MOVE D0,8(A2)
- ;mcode
-
- :mcode >BLIM:
- POP D0
- MOVE D0,D1
- ANDI #7,D1
- MOVE.W D1,20(A2)
- LSR #3,D0
- MOVE D0,12(A2)
- ;mcode
-
- :mcode BLEN:
- MOVE 8(A2),D0
- LSL #3,D0
- OR.W 18(A2),D0
- MOVE 12(A2),D1
- LSL #3,D1
- OR.W 20(A2),D1
- SUB D0,D1
- PUSH D1
- ;mcode
-
- :mcode >BLEN:
- MOVE 8(A2),D0
- LSL #3,D0
- OR.W 18(A2),D0
- ADD (SP)+,D0
- MOVE D0,D1
- ANDI #7,D1
- MOVE.W D1,20(A2)
- LSR #3,D0
- MOVE D0,12(A2)
- ;mcode
-
- :m BSKIP: bpos: self + >bpos: self ;m
-
- :m START: clear: pos clear: bp ;m
- :m NOLIM: nolim: super clear: bl ;m
- :m RESET: start: self nolim: self ;m
-
- :m BSTEP: get: lim get: bl put: bp put: pos nolim: self ;m
- :m <BSTEP: get: pos get: bp put: bl put: lim clear: pos ;m
-
-
- :m ROUNDBPOS: \ Rounds BPOS up to a byte boundary.
- get: bp 0<> -: pos clear: bp ;m
-
- :m ROUNDBLIM:
- get: bl 0<> -: lim clear: bl ;m
-
- :mcode (>NXTNB):
- loc
- \ call debugger
- MOVEM.L D3/D4/D7,-(A7)
- POP D1 ; D1 = #bits
- POP D0 ; D0 = n
- MOVEQ #32,D2
- SUB D1,D2 ; D2 = left shift quantity
- MOVE.W 18(A2),D3 ; D3 = bp
- LSL D2,D0
- LSR D3,D0 ; align n in D0
- MOVEQ #-1,D1
- LSL D2,D1
- LSR D3,D1 ; D1 = aligned mask
- MOVE (A2),A0 ; A0 = handle
- MOVE (A0),A0 ; Dereference it - addr of start of string
- ADD 8(A2),A0 ; Add POS, giving addr of start of active part
- MOVEQ #3,D7
- lp1 LSL #8,D4
- MOVE.B (A0)+,D4
- DBRA D7,lp1
- NOT D1
- AND D1,D4
- OR D0,D4
- MOVEQ #3,D7
- lp2 move.b D4,-(A0)
- LSR #8,D4
- DBRA D7,lp2
- MOVEM.L (A7)+,D3/D4/D7
- ;mcode
-
- :m >NXTNB: { n #bits -- }
- \ Overwrites #bits bits of SELF with n, which is right justified.
- \ Updates BPOS. #bits must be less than 25.
-
- n #bits (>nxtnb): self
- #bits bskip: self ;m
-
-
- :mcode BFIND: \ ( flg -- n b )
- \ Updates BPOS. n is #bits scanned.
- loc
- \ call debugger
- MOVEM.L D3/D4/D7,-(A7)
- MOVE (SP),D1 ; D1 = boolean we're looking for
- SEQ D1 ; Set to inverse for search on not equal
- CLR -(SP) ; For return result
- BSR dic[getit]
- BLE.S failed
- MOVE.B (A0),D7 ; Save 1st char in D7
- MOVE A0,A1 ; and its addr in A1
- MOVE.W 18(A2),D3
- MOVE.W #$00FF,D4
- LSR.W D3,D4
- AND.B D4,(A0)
- NOT.B D4
- AND.B D1,D4
- OR.B D4,(A0)
- MOVEQ #0,D4 ; Set "equal"
- BRA.S lptst
-
- lp CMP.B (A0)+,D1
- lptst DBNE D0,lp
- DBNE D2,lp
- BEQ.S failed
- SUBQ #1,(SP) ; We found it
- SUBQ #1,A0
- MOVE.B (A0),D0
- EOR.B D1,D0
- MOVEQ #-1,D4
- lp2 ADDQ #1,D4
- ROL.B #1,D0
- BCC.S lp2
- BRA.S rslts
-
- failed MOVE 12(A2),A0
- ADD dic[$start],A0
- MOVE.W 20(A2),D4
-
- rslts MOVE 8(A2),D0
- LSL #3,D0
- OR.W 18(A2),D0 ; Old BPOS to D0
- MOVE A0,D1
- SUB dic[$start],D1
- MOVE D1,8(A2) ; Set POS to found posn
- LSL #3,D1
- OR.W D4,D1 ; New BPOS to D1
- MOVE.W D4,18(A2)
- SUB D0,D1
- MOVE D1,4(SP)
- end MOVE.B D7,(A1) ; Restore first char
- MOVEM.L (A7)+,D3/D4/D7
- ;mcode
-
-
- \ :m BSEARCH: { flg \ sav1st savpos -- b }
- \ 1st: self -> sav1st get: pos -> savpos
- \ $ FF00 get: bp >> ^1st: self
- \ flg IF creset 0 ELSE cset -1 THEN
- \ chskip?: self dup
- \ IF ( found )
- \ 1st: self 24 << flg blocate put: bl
- \ get: pos put: lim
- \ savpos put: pos
- \ THEN
- \ sav1st ptr: self savpos + c! ;m \ Restore 1st char
-
- :m DUMP:
- ." bpos:" bpos: self .h ." blim:" blim: self .h cr
- dump: super ;m
-
- ;class
-
- endload
-
- bitstring BB
-
- : GO
- new: bb " hello" put: bb
- get: bb erase 3 skip: bb 4 >nxtc: bb reset: bb ;
-
- : zz release: bb ;
-